# ============================================
# 学生信息管理系统 - 项目初始化脚本
# ============================================

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  学生信息管理系统项目初始化" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 检查Node.js
Write-Host "[1/8] 检查Node.js环境..." -ForegroundColor Yellow
try {
    $nodeVersion = node --version
    Write-Host "✓ Node.js版本: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ 未检测到Node.js，请先安装Node.js 18+" -ForegroundColor Red
    exit 1
}

# 检查MongoDB
Write-Host "`n[2/8] 检查MongoDB服务..." -ForegroundColor Yellow
$mongoService = Get-Service -Name MongoDB -ErrorAction SilentlyContinue
if ($mongoService -and $mongoService.Status -eq 'Running') {
    Write-Host "✓ MongoDB服务正在运行" -ForegroundColor Green
} else {
    Write-Host "! MongoDB服务未运行，请确保已安装并启动MongoDB" -ForegroundColor Yellow
    Write-Host "  参考文档: .\backend\MONGODB_SETUP_GUIDE.md" -ForegroundColor Gray
}

# 检查Git
Write-Host "`n[3/8] 检查Git..." -ForegroundColor Yellow
try {
    $gitVersion = git --version
    Write-Host "✓ Git版本: $gitVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ 未检测到Git，请先安装" -ForegroundColor Red
    Write-Host "  下载地址: https://git-scm.com/" -ForegroundColor Gray
    Write-Host "  参考文档: .\GIT_SETUP_GUIDE.md" -ForegroundColor Gray
    exit 1
}

# 初始化Git仓库
Write-Host "`n[4/8] 初始化Git仓库..." -ForegroundColor Yellow
if (Test-Path ".git") {
    Write-Host "✓ Git仓库已存在" -ForegroundColor Green
} else {
    git init
    Write-Host "✓ Git仓库初始化完成" -ForegroundColor Green
}

# 安装后端依赖
Write-Host "`n[5/8] 安装后端依赖..." -ForegroundColor Yellow
Set-Location backend
if (Test-Path "node_modules") {
    Write-Host "! 后端依赖已存在，跳过安装" -ForegroundColor Yellow
} else {
    npm install
    Write-Host "✓ 后端依赖安装完成" -ForegroundColor Green
}

# 配置环境变量
Write-Host "`n[6/8] 配置环境变量..." -ForegroundColor Yellow
if (Test-Path ".env") {
    Write-Host "! .env文件已存在" -ForegroundColor Yellow
} else {
    $envContent = @"
# 服务器配置
PORT=3001
NODE_ENV=development

# MongoDB配置
MONGODB_URI=mongodb://localhost:27017/student_management

# JWT密钥
JWT_SECRET=your_super_secret_key_change_in_production
JWT_EXPIRES_IN=7d

# 其他配置
BCRYPT_ROUNDS=10
"@
    Set-Content -Path ".env" -Value $envContent
    Write-Host "✓ .env文件创建完成" -ForegroundColor Green
}

Set-Location ..

# 安装前端依赖
Write-Host "`n[7/8] 安装前端依赖..." -ForegroundColor Yellow
Set-Location frontend
if (Test-Path "node_modules") {
    Write-Host "! 前端依赖已存在，跳过安装" -ForegroundColor Yellow
} else {
    npm install
    Write-Host "✓ 前端依赖安装完成" -ForegroundColor Green
}

Set-Location ..

# Git首次提交
Write-Host "`n[8/8] Git首次提交..." -ForegroundColor Yellow
git add .
git commit -m "feat: 初始化学生信息管理系统项目

- 添加项目需求拆解文档
- 配置前后端开发环境
- 创建Git初始化指南
- 配置MongoDB数据库连接" -ErrorAction SilentlyContinue

if ($LASTEXITCODE -eq 0) {
    Write-Host "✓ 代码已提交到本地仓库" -ForegroundColor Green
} else {
    Write-Host "! Git提交失败或无新文件" -ForegroundColor Yellow
}

# 完成提示
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "  初始化完成！" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "`n下一步操作：" -ForegroundColor Yellow
Write-Host "1. 配置Gitee远程仓库（参考GIT_SETUP_GUIDE.md）" -ForegroundColor White
Write-Host "2. 创建功能分支：" -ForegroundColor White
Write-Host "   git checkout -b feature/backend" -ForegroundColor Gray
Write-Host "3. 开始后端开发：" -ForegroundColor White
Write-Host "   cd backend" -ForegroundColor Gray
Write-Host "   npm run dev" -ForegroundColor Gray
Write-Host "4. 查看开发文档：" -ForegroundColor White
Write-Host "   .\ASSIGNMENT_REQUIREMENTS.md" -ForegroundColor Gray
Write-Host ""
